home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / FunTetris / funtetris.jar / gratest / Tetris.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-05-20  |  2.9 KB  |  69 lines

  1. package gratest;
  2.  
  3. import javax.microedition.lcdui.Command;
  4. import javax.microedition.lcdui.CommandListener;
  5. import javax.microedition.lcdui.Display;
  6. import javax.microedition.lcdui.Displayable;
  7. import javax.microedition.lcdui.Form;
  8. import javax.microedition.lcdui.TextField;
  9. import javax.microedition.midlet.MIDlet;
  10. import javax.microedition.midlet.MIDletStateChangeException;
  11.  
  12. public class Tetris extends MIDlet implements CommandListener {
  13.    private Command exitCommand = new Command("Exit", 7, 2);
  14.    private Command newGameCommand = new Command("New", 4, 2);
  15.    private Command returnCommand = new Command("Menu", 4, 2);
  16.    private Display display = Display.getDisplay(this);
  17.    private TetrisCanvas screen;
  18.    private Form form = new Form("Tetris");
  19.    private TextField boxsize = new TextField("Number of boxes:", "12", 2, 2);
  20.    private TextField level = new TextField("Starting level(1-9):", "3", 1, 2);
  21.  
  22.    public Tetris() {
  23.       this.form.append(this.boxsize);
  24.       this.form.append(this.level);
  25.       this.form.addCommand(this.exitCommand);
  26.       this.form.addCommand(this.newGameCommand);
  27.       this.form.setCommandListener(this);
  28.    }
  29.  
  30.    public void startApp() throws MIDletStateChangeException {
  31.       this.display.setCurrent(this.form);
  32.    }
  33.  
  34.    public void pauseApp() {
  35.    }
  36.  
  37.    public void destroyApp(boolean unconditional) {
  38.       this.screen.tetTimer.cancel();
  39.    }
  40.  
  41.    public void commandAction(Command c, Displayable s) {
  42.       if (c == this.exitCommand) {
  43.          this.destroyApp(false);
  44.          ((MIDlet)this).notifyDestroyed();
  45.       } else if (c == this.newGameCommand) {
  46.          int iNum = Integer.parseInt(this.boxsize.getString());
  47.          if (iNum > 20 || iNum < 10) {
  48.             iNum = 12;
  49.          }
  50.  
  51.          int iLevel = Integer.parseInt(this.level.getString());
  52.          if (iLevel > 9 || iLevel < 1) {
  53.             iLevel = 1;
  54.          }
  55.  
  56.          this.screen = new TetrisCanvas(iNum, iLevel);
  57.          this.screen.addCommand(this.exitCommand);
  58.          this.screen.addCommand(this.returnCommand);
  59.          this.screen.setCommandListener(this);
  60.          this.display.setCurrent(this.screen);
  61.       } else if (c == this.returnCommand) {
  62.          this.screen.tetTimer.cancel();
  63.          this.screen = null;
  64.          this.display.setCurrent(this.form);
  65.       }
  66.  
  67.    }
  68. }
  69.